
<template>
  <div class="home-box">
    <el-container>
      <!-- //头部 -->
      <el-header>
        <div class="header-left">
          <img src="@/assets/imgs/shop.png" alt="" />
          <span>管理系统</span>
        </div>
        <div class="header-right">
          <span>欢迎您:{{ userInfo.username }}</span>
          <el-button type="danger" size="mini" @click="loginut">退出</el-button>
        </div>
      </el-header>
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <div class="toggle-button" @click="isCollapse = !isCollapse">
            <i :class="isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'"></i>
          </div>
          <el-menu
            :default-active="$route.path"
            unique-opened
            router
            :collapse="isCollapse"
            :collapse-transition="false"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b"
          >
            <el-submenu
              :index="item.id + ''"
              v-for="item in menuList"
              :key="item.id"
            >
              <template slot="title">
                <i :class="iconObj[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <el-menu-item
                :index="'/' + subItem.path"
                v-for="subItem in item.children"
                :key="subItem.id"
              >
                <i class="el-icon-menu"></i>
                {{ subItem.authName }}</el-menu-item
              >
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 主体 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: null,
      menuList: [],
      //字体图标对象
      iconObj: {
        201: "iconfont icon-shouye",
        125: "iconfont icon-users",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
      },
      //是否折叠收起菜单
      isCollapse: false,
    };
  },
  created() {
    this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
    this.getMenuList();
  },
  methods: {
    // 退出
    loginut() {
      this.$confirm("确定要退出吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          sessionStorage.clear();
          this.$router.push("/login");
        })
        .catch(() => {
          // this.$message({
          //   type: 'info',
          //   message: '已取消删除'
          // });
        });
    },

    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg);
      }

      this.menuList = res.data;
    },
  },
};
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.home-box {
  height: 100%;
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: white;
    background: url(~@/assets/imgs/header_bg.gif) repeat-x center;
    height: 60px;
    .header-left {
      display: flex;
      align-items: center;
      img {
        width: 40px;
        margin-right: 15px;
      }
    }
    .header-right {
      span {
        margin-right: 12px;
      }
    }
  }
  .el-aside {
    background: #545c64;
    .toggle-button {
      background: #60779d;
      font-size: 20px;
      line-height: 26px;
      text-align: right;
      color: white;
      letter-spacing: 0.2rem;
      cursor: pointer;
    }
    .el-menu {
      border-right: 0;
      .iconfont {
        margin-right: 10px;
      }
    }
  }
  .el-main {
    background: #eaedf1;
  }
}
</style>